const http=require('http');
const url=require('url');
const querystring=require('querystring');
const common=require('./buffer');
const uuid=require('uuid/v4');
const fs=require('fs');
		
const path=require('path');//扩展名
		
let server=http.createServer((req,res)=>{
		//get数据
		let {pathname,query}=url.parse(req.url,true);

		// console.log('收到数据:',pathname,query);
		//post数据
		let arr=[];
		req.on('data',data=>{
			arr.push(data);
		});	
		req.on('end',()=>{
			let data=Buffer.concat(arr);
				// console.log(req.headers['content-type'])
				//unencode 普通数据。字符串类型
				//form-data  二进制数据
				// ------WebKitFormBoundaryEyjAJA6hlYewQDAN\r\n
						// Content-Disposition: form-data; name="user"\r\n
						// \r\n
						// 395488790@qq.com\r\n
						// ------WebKitFormBoundaryEyjAJA6hlYewQDAN\r\n
						// Content-Disposition: form-data; name="pass"\r\n
						// \r\n
						// 123412412\r\n
						// ------WebKitFormBoundaryEyjAJA6hlYewQDAN\r\n
						// Content-Disposition: form-data; name="f1"; filename="1.txt"\r\n
						// Content-Type: text/plain\r\n
						// \r\n
						// aaaa
						// bbbbb
						// ------WebKitFormBoundaryEyjAJA6hlYewQDAN--
				//console.log(req.headers['content-type'])
				//multipart/form-data; boundary=----WebKitFormBoundaryDB81OZGlNx7VLHvW
				// console.log(data.toString());
				// console.log(req.headers)
			if(req.headers['content-type'].indexOf('multipart/form-data')!=-1){
				//是二进制数据,逐个分割
				let post={};
				let files=[];
				const boundary='--'+req.headers['content-type'].split('; ')[1].split('=')[1];
				let arra=data.split(boundary);
				arra.shift();
				arra.pop();
					// Content-Disposition: form-data; name="user"\r\n
					// \r\n
					// 395488790@qq.com\r\n
					// ------WebKitFormBoundaryEyjAJA6hlYewQDAN\r\n
					// Content-Disposition: form-data; name="pass"\r\n
					// \r\n
					// 123412412\r\n
					// ------WebKitFormBoundaryEyjAJA6hlYewQDAN\r\n
					// Content-Disposition: form-data; name="f1"; filename="1.txt"\r\n
					// Content-Type: text/plain\r\n
					// \r\n
					// aaaa
					// bbbbb

				arra=arra.map(item=>item.slice(2,item.length-2));

				arra.forEach(item=>{
					let m=item.indexOf('\r\n\r\n');//逐个遍历，分割
					let info=item.slice(0,m);//信息部分
					let data=item.slice(m+4);//数据部分
						info=info.toString();

					let total=0;
					let complete=0;

					// console.log(infoo,data)
					
					if(info.indexOf('\r\n')==-1){//普通数据
						let key=common.parseInfo(info).name;
						let val=data.toString();
						post[key]=val;
						// console.log(key)
					}else{//文件数据
						total++;
						let json=common.parseInfo(info);
						let key=json.name;
						let filenamee=json.filename;
						let type=json["Content-Type"];
						let val=data;

						let filepath=`../upload/${uuid().replace(/\-/g,'')}${path.extname(filenamee)}`;

						files[key]={filenamee, type ,filepath};

						console.log(filenamee);

						fs.writeFile(filepath,data,err=>{
							if(err){
								console.log('文件呢写入有误')
							}else{
								console.log('写入成功');
								complete++;
								console.log(post,files)
							}
						});
						console.log('complete')
					}
				});
			}else{
				//不存在就是普通数据
				let post=querystring.parse(data.toString());
				console.log('post数据:',post);
			}
		});

	});

server.listen(8080);




